package edu.cn.nlsde.tmfst.model;

import edu.cn.nlsde.tmfst.config.DBUitil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by dell on 2017/3/9.
 */
public class Douban_topic {
    public String topic_id = "";
    public Date create_time = new Date();
    public int is_crawled = 0;

    public void save_model() {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            String sql = "insert IGNORE into Douban_topic(topic_id,create_time,is_crawled) values(?,?,?)";
            con = DBUitil.openConnection();
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, this.topic_id);
            pstmt.setTimestamp(2, new java.sql.Timestamp(this.create_time.getTime()));
            pstmt.setInt(3, this.is_crawled);
            pstmt.executeUpdate();
        } catch (SQLException ex) {
        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static List<Douban_topic> get_comment() {
        List<Douban_topic> results = new ArrayList<>();
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DBUitil.openConnection();
            String sql = "select * from Douban_topic where is_crawled = 0";
            pstmt = con.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Douban_topic oneItem = new Douban_topic();
                oneItem.topic_id = rs.getString("topic_id");
                oneItem.is_crawled = rs.getInt("is_crawled");
                oneItem.create_time = rs.getTimestamp("create_time");
                results.add(oneItem);
            }
        } catch (SQLException ex) {

        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return results;
    }
}
